package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Menu;
import com.mls.fivedistrict.server.MenuService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * Created by nanji on 2018/3/20.
 */
@RestController("backendMenuController")
@RequestMapping("/api/manager")
public class MenuController extends BaseController {
    @Autowired
    MenuService menuService;

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("/v1/menu")
    public ServiceResponse addMenu(@RequestBody Menu menu){
        BeanValidator.check(menu);
        boolean insert = menuService.insert(menu);
        if(insert){
            return ServiceResponse.createSuccessResponse("添加成功");
        }else{
            return ServiceResponse.createErrorResponse("添加失败");
        }
    }

    /**
     * 修改菜单
     * @param menuId
     * @param menu
     * @return
     */
    @PutMapping("/v1/menu/{menuId}")
    public ServiceResponse editMenu(@PathVariable Long menuId,@RequestBody Menu menu){
        Optional.ofNullable(menuId).orElseThrow(() -> new CustomerException("菜单id不能为空"));
        Optional.ofNullable(menu).orElseThrow(() -> new CustomerException("菜单不能为空"));
        menu.setId(menuId);
        boolean update = menuService.updateById(menu);
        if(update){
            return ServiceResponse.createSuccessResponse("修改成功");
        }else{
            return ServiceResponse.createErrorResponse("修改失败");
        }
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping("/v1/menu/{menuId}")
    public ServiceResponse deleteMenu(@PathVariable Long menuId){
        Optional.ofNullable(menuId).orElseThrow(() -> new CustomerException("菜单id不能为空"));
        Menu menu = new Menu();
        menu.setId(menuId);
        menu.setDelete(true);
        boolean delete = menuService.updateById(menu);
        if(delete){
            return ServiceResponse.createSuccessResponse("删除成功");
        }else{
            return ServiceResponse.createErrorResponse("删除失败");
        }
    }

    /**
     * 菜单列表
     * @return
     */
    @GetMapping("/v1/menus")
    public ServiceResponse getMenuList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = menuService.selectList(pageFilter.getWrapper());
        PageInfo<Menu> menuPageInfo = new PageInfo<Menu>(selectList, 5);
        return ServiceResponse.createSuccessResponse(menuPageInfo);
    }

    /**
     * 单个菜单详情
     * @return
     */
    @GetMapping("/v1/menu/{menuId}")
    public ServiceResponse getMenu(@PathVariable Long menuId){
        Optional.ofNullable(menuId).orElseThrow(() -> new CustomerException("菜单id不能为空"));
        Menu menu = menuService.selectById(menuId);
        if(null!=menu){
            return ServiceResponse.createSuccessResponse(menu);
        }else{
            return  ServiceResponse.createErrorResponse("查无此菜单");
        }
    }

}
